package com.ctgu.fcftsystem.controller;

import com.ctgu.fcftsystem.pojo.Revenue;
import com.ctgu.fcftsystem.service.RevenueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/RevenueController")
public class RevenueController {
    @Autowired
    private RevenueService revenueService;

    /**
     * 根据时间对收入记录进行查询
     *
     * @param revenueDate
     * @return
     */
    @RequestMapping("/searchRevenueByDate")
    public Map searchRevenueByDate(String revenueDate) {
        if(revenueDate==null||revenueDate.length()==0){
            Map<String, Object> mcp = new HashMap<>();
            mcp.put("code", 1);
            mcp.put("msg", "传入参数为空");
            mcp.put("data", -1);
            return mcp;
        }
        Map<String, Object> map = new HashMap<>();

        DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        Date date = null;
        try {
            date = format.parse(revenueDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        List<Revenue> revenueList = revenueService.searchRevenueByDate(date);
        if (revenueList != null&&revenueList.size()>0) {
            map.put("code", 0);
            map.put("msg", "查询成功");
            map.put("data", revenueList);
        }
        else{
            map.put("code", 1);
            map.put("msg", "所查询的结果不存在");
            map.put("data", revenueList);
        }


            return map;
    }
    /**
     * 根据类型对收入记录进行查询
     * @param revenueType
     * @return
     */
    @RequestMapping("/searchRevenueByType")
    public Map searchRevenueByType(Integer revenueType){
        Map<String, Object> map = new HashMap<>();


        List<Revenue> revenueList = revenueService.searchRevenueByType(revenueType);
        if (revenueList != null&&revenueList.size()>0) {
            map.put("code", 0);
            map.put("msg", "查询成功");
            map.put("data", revenueList);
        }
        else{
            map.put("code", 1);
            map.put("msg", "所查询的结果不存在");
            map.put("data", revenueList);
        }


        return map;
    }
}